package com.jboa.web.action;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.jboa.service.IBizClaimVoucherService;
import com.jboa.service.IEmployeeService;
import com.jboa.service.ILeaveService;
import com.jboa.vo.BizClaimVoucher;
import com.jboa.vo.BizClaimVoucherDetail;
import com.jboa.vo.BizLeave;
import com.jboa.vo.SysEmployee;
import com.opensymphony.xwork2.ActionContext;

public class StaffAction {
	@Autowired
	@Qualifier("voucherService")
	private IBizClaimVoucherService service;
	@Autowired
	@Qualifier("employeeService")
	private IEmployeeService serviceEmp;
	@Autowired
	@Qualifier("LeaveService")
	private ILeaveService serviceLeave;
	private BizLeave leave;
	private BizClaimVoucher voucher;
	private BizClaimVoucherDetail detail;
	private List<BizClaimVoucherDetail> detailList;

	/**
	 * 获取报销单详情
	 */
	public String detail() {
		voucher = service.getDetailById(voucher.getId());
		return "success";
	}

	/**
	 * 添加报销单
	 */
	public String addVoucher() {
		// 下一个处理报销单的人
		if (voucher.getStatus().equals("新创建")) {
			voucher.setSysEmployeeByNextDealSn(voucher
					.getSysEmployeeByCreateSn());
		} else {
			SysEmployee empsession=(SysEmployee) ActionContext.getContext().getSession().get("employee");
			SysEmployee emp = serviceEmp.getNextDealEmployee("部门经理", empsession.getSysDepartment());
			voucher.setSysEmployeeByNextDealSn(emp);
		}
		Set<BizClaimVoucherDetail> details=new HashSet<BizClaimVoucherDetail>();
		for(int i=0;i<detailList.size();i++){
			detailList.get(i).setBizClaimVoucher(voucher);
			details.add(detailList.get(i));
		}
		voucher.setBizClaimVoucherDetails(details);
		
		if (service.add(voucher)) {
			return "success";
		}
		return "input";
	}

	/**
	 * 修改报销单
	 */
	public String updateVoucher() {
		SysEmployee loginEmp=(SysEmployee) ActionContext.getContext().getSession().get("employee");
		voucher.setSysEmployeeByCreateSn(loginEmp);
		// 下一个处理报销单的人
		if (voucher.getStatus().equals("新创建")) {
			voucher.setSysEmployeeByNextDealSn(loginEmp);
		} else {
			SysEmployee emp = serviceEmp.getNextDealEmployee("部门经理", loginEmp.getSysDepartment());
			voucher.setSysEmployeeByNextDealSn(emp);
		}
		
		Set<BizClaimVoucherDetail> details=new HashSet<BizClaimVoucherDetail>();
		for(int i=0;i<detailList.size();i++){
			detailList.get(i).setBizClaimVoucher(voucher);
			details.add(detailList.get(i));
		}
		voucher.setBizClaimVoucherDetails(details);
		if (service.update(voucher)) {
			return "success";
		}
		return "input";
	}
	
	/**
	 * 添加请假单
	 * 
	 * @return
	 */
	public String addLeave() {
		leave.setCreatetime(new Date());
		leave.setStatus("待审批");
		if (serviceLeave.addLeave(leave)) {
			return "success";
		}
		return "input";
	}
	
	/**
	 * 获取请假单详情
	 * 
	 * @return
	 */
	public String getLeaveDetail(){
		leave=serviceLeave.getByid(leave.getId());
		return "success";
	}

	public BizClaimVoucher getVoucher() {
		return voucher;
	}

	public void setVoucher(BizClaimVoucher voucher) {
		this.voucher = voucher;
	}

	public BizClaimVoucherDetail getDetail() {
		return detail;
	}

	public void setDetail(BizClaimVoucherDetail detail) {
		this.detail = detail;
	}

	public List<BizClaimVoucherDetail> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<BizClaimVoucherDetail> detailList) {
		this.detailList = detailList;
	}

	public BizLeave getLeave() {
		return leave;
	}

	public void setLeave(BizLeave leave) {
		this.leave = leave;
	}

	
}
